/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.statistics.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.service.BookStoreService;
import com.thinkgem.jeesite.modules.borrow.entity.Borrow;
import com.thinkgem.jeesite.modules.borrow.service.BorrowService;
import com.thinkgem.jeesite.modules.reader.service.ReadersService;
import com.thinkgem.jeesite.modules.school.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 流程个人任务相关Controller
 *
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/statistics")
public class StatisticsController extends BaseController{
    @Autowired
    private BookStoreService bookStoreService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private ReadersService readersService;

    @ModelAttribute
    public Borrow get(@RequestParam(required = false) String id){
        if(StringUtils.isNotBlank(id)){
            return borrowService.get(id);
        }
        else{
            return new Borrow();
        }
    }

    /**
     * 读者借阅统计
     *
     * @param type
     * @param begin
     * @param end
     * @param model
     * @return
     */
    @RequestMapping("reader")
    public String reader(String type, String begin, String end, Model model){
        List<Map<String, Integer>> oneDayBorrowUserAmount = borrowService.getreaderStatistics(type, begin, end);
        model.addAttribute("oneDayBorrowUserAmount", oneDayBorrowUserAmount);
        model.addAttribute("type", type);
        model.addAttribute("begin", begin);
        model.addAttribute("end", end);
        return "modules/statistics/reader";
    }

    /**
     * 日借阅量统计
     *
     * @param date
     * @param model
     * @return
     */
    @RequestMapping("borrow")
    public String borrow(String date, Model model){
        List<Map<String, Integer>> oneDayBorrowUserAmount = borrowService.getOneDayBorrowUserAmount(date);
        model.addAttribute("oneDayBorrowUserAmount", oneDayBorrowUserAmount);
        model.addAttribute("date", date);
        return "modules/statistics/borrow";
    }

    /**
     * 图书利用率
     *
     * @param model
     * @return
     */
    @RequestMapping("use")
    public String use(String begin, String end, Model model) throws ParseException{
        List<Map<String, Object>> utilizationToTal = borrowService.utilizationToTal(begin, end);
        List<Map<String, Object>> utilizationBook = borrowService.utilizationBook(begin, end);
        List<Map<String, Object>> utilizationReader = borrowService.utilizationReader(begin, end);
        int totalBooks = 0;
        int totalBooksBorrow = 0;
        int totalReader = 0;
        Map<String, Object> total = new LinkedHashMap<>();
        for(Map<String, Object> item : utilizationToTal){
            Map<String, Object> detail = new HashMap<>();
            Long amount = (Long)item.get("amount");
            totalBooks += amount;
            detail.put("total", amount);

            total.put((String)item.get("classcode"), detail);
        }

        for(Map<String, Object> item : utilizationBook){
            String key = (String)item.get("classcode");
            Map<String, Object> keyMap = (Map<String, Object>)total.get(key);

            if(null == keyMap){
                keyMap = new HashMap<>();
            }

            Long amount = (Long)item.get("amount");
            totalBooksBorrow += amount;

            keyMap.put("book", amount);

            total.put(key, keyMap);
        }

        for(Map<String, Object> item : utilizationReader){
            String key = (String)item.get("classcode");
            Map<String, Object> keyMap = (Map<String, Object>)total.get(key);
            Long amount = (Long)item.get("amount");
            totalReader += amount;
            keyMap.put("reader",amount);
        }
//        if(StringUtils.isNotEmpty(begin) && StringUtils.isNotEmpty(end)){
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Date date1 = sdf.parse(begin);
//            Date date2 = sdf.parse(end);
//
//            int days = (int)((date2.getTime()-date1.getTime()) / 86400000)+1;
//            DecimalFormat df   = new DecimalFormat("######0.000");
//
//            double d1 = (totalBooksBorrow / (totalBooks * days)) * 100;
//            df.format(d1);
//            model.addAttribute("use", df.format(d1));
//        }

        DecimalFormat df   = new DecimalFormat("######0.000");
        double d1 = (totalBooksBorrow / totalBooks ) * 100;
        df.format(d1);
        model.addAttribute("use", df.format(d1));
        //重置第一行求和数据
        Map<String, Object> totalFirst = (Map<String, Object>)total.get(null);
        totalFirst.put("total",totalBooks);
        totalFirst.put("book",totalBooksBorrow);
        totalFirst.put("reader",totalReader);
        model.addAttribute("total", total);
        model.addAttribute("begin", begin);
        model.addAttribute("end", end);
        return "modules/statistics/use";
    }

    /**
     * 违约统计
     *
     * @param model
     * @return
     */
    @RequestMapping("outLine")
    public String peccancy(String begin, String end, Model model) throws ParseException{
        String now = DateUtils.getDate();
        List<Map<String, Object>> outLine = borrowService.outLine(begin, end, now);

        model.addAttribute("outLine", outLine);
        model.addAttribute("begin", begin);
        model.addAttribute("end", end);
        return "modules/statistics/peccancy";
    }

    /**
     * 图书借阅统计
     *
     * @param model
     * @return
     */
    @RequestMapping("book")
    public String book(String begin, String end, String type, Model model) throws ParseException{
        List<Map<String, Object>> statistics = null;
        if("type".equals(type)){
            statistics = borrowService.bookBorrowStatisticsByType(begin, end);
        }
        else if("level".equals(type)){
            statistics = borrowService.bookBorrowStatisticsByLevel(begin, end);
        }
        else if("other".equals(type)){
            statistics = borrowService.bookBorrowStatisticsByOther(begin, end);
        }

        model.addAttribute("statistics", statistics);
        model.addAttribute("begin", begin);
        model.addAttribute("end", end);
        return "modules/statistics/book";
    }
    /**
     * 按读者类型查看单个类型详细
     *
     * @param date
     * @param model
     * @param roleid
     * @param source
     * @return
     */
    @RequestMapping("readerdetail")
    public String borrow(String date, String roleid,String source, Model model){
        Borrow borrow = new Borrow();
        borrow.setRdRoleId(roleid);
        borrow.setBorDate(DateUtils.parseDate(date));
        borrow.setSource(source);
        List<Borrow> retBorrow = borrowService.findBorrowListByReaderType(borrow);
        model.addAttribute("retborrowlist", retBorrow);
        return "modules/statistics/readerdetail";
    }
}
